package leetcode;

public class JumpGame {

    public static void main(String[] args) {
        JumpGame jumpGame = new JumpGame();
        boolean b = jumpGame.canJumpOpt(new int[]{2,3,1,1,4});
        System.out.println(b);
    }

    public boolean canJumpOpt(int[] nums) {
        int n = nums.length;
        int longest = 0;
        for (int i = 0; i <= longest && longest < n - 1; i++) {
            longest = Math.max(longest, i + nums[i]);
        }
        return longest >= n - 1;
    }
    public boolean canJumpForce(int[] nums) {
        int n = nums.length;
        int longest = 0;
        for (int i = 0; i < n; i++) {
            if(i > longest){
                return false;
            }
            longest = Math.max(longest, i + nums[i]);
        }
        return true;
    }


}
